package com.example.fechat.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.fechat.R
import com.example.fechat.activity.MessageActivity
import com.example.fechat.adapter.ContactsAdapter
import com.example.fechat.base.BaseAdapter
import com.example.fechat.bean.ChatBean
import com.example.fechat.room.user.UserDBUtils
import com.example.fechat.room.user.UserEntity
import com.example.fechat.utils.Files

class ContactsFragment : Fragment() {
    private lateinit var recyclerView: RecyclerView
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_contacts, container, false)
        recyclerView = view.findViewById(R.id.recyclerView)
        initData()
        val data = ArrayList<UserEntity>(UserDBUtils.getAll(context))
        recyclerView.layoutManager = LinearLayoutManager(context)
        val adapter = ContactsAdapter(data)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(object : ContactsAdapter.OnItemClickListener{
            override fun onItemClick(view: View, position: Int) {
                val intent = Intent(context, MessageActivity::class.java)
                intent.putExtra("UserInfo", data[position].toString())
                startActivity(intent)
            }

        })
        return view
    }

    private fun initData() {
        val data = UserDBUtils.getAll(context)
        if (data.isNotEmpty())
            return
        for (i in 0..20) {
            val userEntity = UserEntity()
            userEntity.userName = "用户$i"
            userEntity.userId = "00000$i"
            userEntity.userHead = "$i"
            UserDBUtils.insertUser(context, userEntity)
        }
    }
}